package com.gdatacloud.ai;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws DocumentException, IOException {
        test();
    }

    public static void test() throws DocumentException, IOException {
        Map<String, Object> data = new HashMap<>();//要插入的数据
        //初始化itext
        //设置编码
        BaseFont baseFont = BaseFont.createFont("D:\\privacy\\wx\\WeChat Files\\wxid_9188411883512\\FileStorage\\File\\2024-05\\SanJiJinSongJianTi-Xi-2.ttf",  BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font =new Font(baseFont,12f,Font.NORMAL, BaseColor.BLACK);
        PdfReader pdfReader=new PdfReader("D:\\data1\\hanwei\\执法文书模板\\1.pdf");
        PdfStamper pdfStamper=new PdfStamper(pdfReader, new FileOutputStream("D:\\data1\\hanwei\\执法文书模板\\2.pdf"));
        AcroFields form = pdfStamper.getAcroFields();
        form.addSubstitutionFont(baseFont);
        data.put("entBusinessinfo.entname", "123");
        data.put("startTimeMap.year", "2024");
        data.put("lawCheckrecord.exacondition", "1、安全措施不到位\n" +
                "2、安全措施不到位\n" +
                "3、安全措施不到位\n" +
                "4、安全措施不到位\n" +
                "5、安全措施不到位\n" +
                "6、安全措施不到位");
        //写入数据
        for(String key:data.keySet()){
            String value=data.get(key).toString();
            //key对应模板数据域的名称
            form.setFieldProperty(key, "textfont", baseFont, null);
            form.setField(key,value);
        }

        //添加图片
//        int pageNo = form.getFieldPositions("img").get(0).page;
//        Rectangle signRect = form.getFieldPositions("img").get(0).position;
//        float x = signRect.getLeft();
//        float y = signRect.getBottom();
//        Image image = Image.getInstance("D:\\data1\\hanwei\\park-law-check\\main\\webapps\\gbsafe\\target\\zwsafe_uploadFiles\\Group 26_1716796286370.png");
//        PdfContentByte under = pdfStamper.getOverContent(pageNo);
//        //设置图片大小
//        image.scaleAbsolute(signRect.getWidth(), signRect.getHeight());
//        //设置图片位置
//        image.setAbsolutePosition(x, y);
//        under.addImage(image);

        //设置不可编辑
        pdfStamper.setFormFlattening(true);
        pdfStamper.close();
    }
}